$(function(){	
	
	
	//绑定附件上传组件
	Otod.file.init($("#taskFileUpload")[0],{
		clickable:true,
		sending: function(file, xhr, formData){
			$("#fileActive").show();
			var uuid = $("#taskModen input[name='uuid']").val();
			formData.append("busiId", uuid);
		},
		success: function(file, data, e) {
			showTaskAttach();
			$("#fileActive").hide();
			document.getElementById("progress").style.width = 0 + "%";
		},
		error: function(file, message){
			Otod.Msg.error(message.error);
			$("#fileActive").hide();
			document.getElementById("progress").style.width = 0 + "%";
		},
		//进度条
		uploadprogress:function(file,progress, bytesSent) {
			
			document.getElementById("progress").style.width = progress + "%";
		}
	});	
	
	//绑定附件上传组件
	Otod.file.init($("#comment-upload-doc-task")[0],{
		clickable:true,
		sending: function(file, xhr, formData){
			$("#file-active-comment-task").show();
			var uuid = $("#taskModen input[name='uuid']").val();
			formData.append("busiId", uuid);
		},
		success: function(file, data, e) {
			showTaskAttach();
			$("#file-active-comment-task").hide();
			document.getElementById("progress-task").style.width = 0 + "%";
		},
		error: function(file, message){
			Otod.Msg.error(message.error);
			$("#file-active-comment-task").hide();
			document.getElementById("progress-task").style.width = 0 + "%";
		},
		//进度条
		uploadprogress:function(file,progress, bytesSent) {
			
			document.getElementById("progress-task").style.width = progress + "%";
		}
	});	
	
	$("#form-field-select-4").chosen(); 
	
	/**
	 * 查看修改的历史记录
	 */
	$("#showRecord").on('click',function(e){
	//	e.preventDefault();
		
		showUpdateRecord($("#taskId").val());
	});
	
	//任务修改
	$(".step-content-cfg").on('click',".item-btn-edit",function(e){
		e.preventDefault();
		editTask(this);
	});
	
	//任务查看
	$(".step-content-cfg").on('click',".item-btn-view",function(e){
	
		e.preventDefault();
		editTask(this);
	});
	
	
	
	//任务修改时间
	$("#memberButton").on('click',".taskModen-btn-save",function(e){
		updateTask();
	});
	/**
	 * 任务修改保存
	 */
	function updateTask(){
		
		// 任务结束时间
		if ($('#taskModen-form input[name=endDate]').val()) {
			var endDate = DateUtil.dateToLong(DateUtil.strToDate($('#taskModen-form input[name=endDate]').val()));
			if (endDate > GLOBAL_PRJ.endDate) {
				Otod.Msg.info("任务的结束时间 不能大于项目的结算时间  当前项目结束时间为"+DateUtil.dateFormat(Number(GLOBAL_PRJ.endDate)));
				return;
			}
		}
		Otod.ajax({
			url: ctx + '/pm/task/update.do',
			data : $('#taskModen-form').serialize(),
			contentType : "application/x-www-form-urlencoded;",
			type:"POST",
			successFn : function(data) {
				
				$('#taskModen').modal('hide');
				refreshTask(data);
				//$('#task-item-panel-'+data.uuid + " label span").text(data.taskName);
				
				location.reload();
			}
		});
	
	}
	
	
	
	//项目经理确认任务完成
	$("#memberButton").on('click',".taskModen-btn-auditComplete",function(e){
		e.preventDefault();
		
		$("#submitComment-form").removeClass("hidden");
		$(".taskModen-btn-auditComplete").addClass("hidden");
		$(".comment-btn-confirm").attr('path',$(this).attr('path'));
//		Otod.Msg.confirm("确定要确认任务完成吗？", "确认完成",function(){
//			
//			Otod.ajax({
//				url: ctx + '/pm/task/auditComplete.do',
//				data:{taskId:$("#taskModen input[name='uuid']").val(),comment:$("#submitComment-form textarea[name='submitComment']").val(),prjId:prjId},
//				type:"GET",
//				successFn : function(data) {
//					$('#taskModen').modal('hide');
//					Otod.msg.info(data.info);
//					
//				}
//			});
//		});
	});
	
	//成员提交任务
	$("#memberButton").on('click',".taskModen-btn-complete",function(e){
		e.preventDefault();
		$("#submitComment-form").removeClass("hidden");
		$(".taskModen-btn-complete").addClass("hidden");
		$(".comment-btn-confirm").attr('path',$(this).attr('path'));
//		Otod.Msg.confirm("确定要提交任务吗？", "提交任务",function(){
//			
//			Otod.ajax({
//				url: ctx + '/pm/task/complete.do',
//				data:$("#taskModen input[name='uuid']").val(),
//				successFn : function(data) {
//					$('#taskModen').modal('hide');
//					
//				}
//			});
//		});
		
		
	});
	//成员备注及提交任务
	$("#submitComment-form").on('click',".comment-btn-confirm",function(e){
		e.preventDefault();
		var path = $(this).attr('path');
		Otod.Msg.confirm("确定并提交？", "操作",function(){
			Otod.ajax({
				url: ctx + path,
				data:{taskId:$("#taskModen input[name='uuid']").val(),comment:$("#submitComment-form textarea[name='submitComment']").val(),prjId:prjId},
				type:"GET",
				successFn : function(data) {
					$('#taskModen').modal('hide');
					Otod.Msg.info(data.info);
					//TODO 任务列表需要刷新
				}
			});
		});
		
		
	});
	//取消提交
	$("#submitComment-form").on('click',".comment-btn-return",function(e){
		e.preventDefault();
		$("#submitComment-form").addClass("hidden");
		$(".taskModen-btn-complete").removeClass("hidden");
		$(".taskModen-btn-auditComplete").removeClass("hidden");
		$(".taskModen-btn-rejectTask").removeClass("hidden");
		
		
	});
	
	
	
	//项目经理退回任务
	$("#memberButton").on('click',".taskModen-btn-rejectTask",function(e){
		e.preventDefault();
		$("#submitComment-form").removeClass("hidden");
		$(".taskModen-btn-rejectTask").addClass("hidden");
		$(".comment-btn-confirm").attr('path',$(this).attr('path'));
//		Otod.Msg.confirm("确定要驳回任务吗？", "驳回任务",function(){
//			Otod.ajax({
//				url: ctx + '/pm/task/rejectTask.do',
//				data:{taskId:$("#taskModen input[name='uuid']").val(),comment:$("#submitComment-form textarea[name='submitComment']").val(),prjId:prjId},
//				type:"GET",
//				successFn : function(data) {
//					
//					$('#taskModen').modal('hide');
//					
//				}
//			});
//		});
		
	});
	
	//附件下载
	$("#taskFileList").on("click",".fileItem",function(e){
		e.preventDefault();
		var fileId = $(this).attr("key");
		Otod.file.download(fileId);
	});
	
	//附件删除
	$("#taskFileList").on("click",".fileItemDel",function(e){
		e.preventDefault();
		var fileId = $(this).attr("key");
		Otod.Msg.confirm("确定要删除该附件吗？", "删除附件",function(){
			Otod.file.del(fileId,showTaskAttach);
		});
	});
	//附件下载
	$("#comments-doc-list").on("click",".fileItem",function(e){
		e.preventDefault();
		var fileId = $(this).attr("key");
		Otod.file.download(fileId);
	});
	
	//附件删除
	$("#comments-doc-list").on("click",".fileItemDel",function(e){
		e.preventDefault();
		var fileId = $(this).attr("key");
		Otod.Msg.confirm("确定要删除该附件吗？", "删除附件",function(){
			Otod.file.del(fileId,showFruitAttach);
		});
	});
	
	
	
	//选择文档编写显示按钮
	$("#taskType").on("change",function(e){
		
		if($(this).val()=="30040003"&&GOBAL_PRJ_ROLE == "30100001"&&$("#status").val() != "30050003"&&$("#status").val() != "30050004"){			
			$(".doc-select-model").show();
			
		}else{
			$(".doc-select-model").hide();	
		}
	});
	
	$("#projectType").on("change",function(e){
		if($(this).val()==""){
			$("#spanTpItem").html("新建文档");
		}else{
			$("#spanTpItem").html("选择章节");
		}
	})
	
	//跳转到模板选择页面
	$("#selectTpModel button").on("click",function(e){	
		
		
		/*Otod.ajax({
			url: ctx + '/pm/task/update.do',
			data : $('#taskModen-form').serialize(),
			contentType : "application/x-www-form-urlencoded;",
			type:"POST",
			successFn : function(data) {
				
				$('#taskModen').modal('hide');
				refreshTask(data);
				//$('#task-item-panel-'+data.uuid + " label span").text(data.taskName);
				
			//	
			}
		});*/
	
	
		window.location.href=ctx+'/jsp/pm/prj/doc/docItemMaster.jsp?prjId='+prjId+'&stepId='+$("#taskModen [name='stepId']").val()+'&prjType='+GLOBAL_PRJTYPE+'&taskId='+$("#taskId").val()+'&taskType='+$("#taskType").val()+'&tpId='+$("#projectType").val();
	});
	
	//selectTpPrjType();
	
	
});	
	//修改step
	function editTask(btn){
		
		resetTaskModen();
		var uuid = $(btn).attr("key");
		//设置阶段id
		$("#taskId").val(uuid);
		Otod.ajax({
			url: ctx + '/pm/task/getById.do',
			data : uuid,
			successFn : function(data) {
				//设置当前任务的状态
				$("#status").val(data.status);
				initTaskPanel(data);
				showMemberButton(data);
				showModalFollowRight(btn,"taskModen");
				initTaskModelStting();
				
				showTaskAttach();
			}
		});
	}
	
	function initEmpSelect(prjId){
		Otod.ajax({
			url: ctx + '/pm/team/findByPrjId.do?prjId='+prjId,
			successFn : function(data) {
				var html = new StringBuffer();
				
					html.append("<option value=\"\">").append("请选择").append("</option>");
				
				for(var i=0;i<data.length;i++){
					html.append("<option value=\"").append(data[i].empNo).append("\" ");
					
					html.append(" >").append(data[i].empName).append("</option>");
				}
				
				$("#form-field-select-4").html(html.toString());
			}
		});
		
	}
	
	function initTaskPanel(data){
		$("#taskModen [name='uuid']").val(data.uuid);
		$("#busitableKey").val(data.uuid);
		$("#taskModen [name='prjId']").val(data.prjId);
		$("#taskModen [name='stepId']").val(data.stepId);
		$("#taskModen [name='projectType']").val(data.docName);
		$("#taskModen [name='taskName']").val(data.taskName);
		$("#taskModen [name='taskType']").val(data.taskType);
		$("#taskModen [name='empNo']").val(data.empNo);
		
		$("#form-field-select-4").val(data.empNosList);
		$("#form-field-select-4").trigger("chosen:updated");
		
		$("#taskModen [name='itemName']").val(data.itemName);
		$("#taskModen [name='depict']").val(data.depict);
		$("#taskModen [name='endDate']").val(DateUtil.dateFormat(data.planEndDate));
		$("#taskModen [name='startDate']").val(DateUtil.dateFormat(data.startDate));
		$("#taskModen [name='taskName']").removeAttr("readonly");
		$("#taskModen [name='taskType']").removeAttr("disabled");
		$("#taskModen [name='depict']").removeAttr("readonly");
		$("#taskModen [name='endDate']").removeAttr("disabled");
		$("#taskModen [name='startDate']").removeAttr("disabled");
		$("#taskModen [name='empNo']").removeAttr("disabled");
		$("#taskModen [name='empNos']").removeAttr("disabled");
		if(GLOBAL_PRJ.status !="30090001" || GOBAL_PRJ_ROLE!="30100002"){//准备中项目，项目负责人
			$("#taskModen [name='taskName']").attr("readonly","true");
			$("#taskModen [name='taskType']").attr("disabled","disabled");
			$("#taskModen [name='itemName']").attr("readonly","true");
			$("#taskModen [name='depict']").attr("readonly","true");
			$("#taskModen [name='endDate']").attr("disabled","disabled");
			$("#taskModen [name='startDate']").attr("disabled","disabled");
			$("#taskModen [name='empNo']").attr("disabled","disabled");
			$("#taskModen [name='empNos']").attr("disabled","disabled");
		}
		
		if(data.empNo==GOBAL_CUST_NO && (data.status == "30050002"||data.status == "30050005")&& GLOBAL_PRJ.status=="30090002"){
			$(".task-upload-control").css("display","");//文件上传控制
			$(".fileItemDel").css("display","");
		}else{
			$(".task-upload-control").css("display","none");//文件上传控制
			$(".fileItemDel").css("display","none");
		}
		
		if(GOBAL_PRJ_ROLE=="30100007"){
			if(GLOBAL_PRJ.status =='30090001'&&GLOBAL_PRJ.status =='30090004'){
				$("#taskModen [name='taskName']").attr("readonly","true");
				$("#taskModen [name='taskType']").attr("disabled","disabled");
				$("#taskModen [name='itemName']").attr("readonly","true");
				$("#taskModen [name='depict']").attr("readonly","true");
				$("#taskModen [name='endDate']").attr("disabled","disabled");
				$("#taskModen [name='startDate']").attr("disabled","disabled");
				$("#taskModen [name='empNo']").attr("disabled","disabled");
				$("#taskModen [name='empNos']").attr("disabled","disabled");
			}else{
				$("#taskModen [name='taskName']").removeAttr("readonly");
				$("#taskModen [name='taskType']").removeAttr("disabled");
				$("#taskModen [name='depict']").removeAttr("readonly");
				$("#taskModen [name='endDate']").removeAttr("disabled");
				$("#taskModen [name='startDate']").removeAttr("disabled");
				$("#taskModen [name='empNo']").removeAttr("disabled");
				$("#taskModen [name='empNos']").removeAttr("disabled");
			}
		}
	}

	//显示任务修改按钮
	function showMemberButton(data){
		var MemberButtonMtp = $('#taskMemberButtonMtp').html();
		Mustache.parse(MemberButtonMtp);
		
		var rendered = Mustache.render(MemberButtonMtp,{
			data:data,
			isPrincipal: function(){
				if(GOBAL_PRJ_ROLE == "30100007"){
					if(GLOBAL_PRJ.status !='30090001'&&GLOBAL_PRJ.status !='30090004'){
						return true;
					}else{return false;}
				}else{
					return GOBAL_PRJ_ROLE == "30100002" && GLOBAL_PRJ.status == "30090001";//未完成
				}
			},
			isSubmit: function(){
				return GOBAL_PRJ_ROLE == "30100002" && data.status == "30050003";//提交状态
			},
			isMember: function(){
				return this.empNo == GOBAL_CUST_NO && (data.status == "30050002"||data.status == "30050005");//进行中
			}
		});
		$('#memberButton').html(rendered);
	}

	//初始化任务model设置
	function initTaskModelStting(){
		initCommentList("");
		$("#tableTurnTag div").removeClass("active");
		$("#home2").addClass("in active");
		$("#myTab2 li").removeClass("active");
		$("#myTab2 li").first().addClass("active");
		$("#comment-form-div").css("display","none");//隐藏
	}

	//重置step moden
	function resetTaskModen(){
		$("#taskModen-form")[0].reset();
		$("#taskModen-form [name=prjId]").val(prjId);		//初始化prjId
		$("#taskFileList").html("");						//清空附件列表
	}
	
	//显示任务附件
	function showTaskAttach(){
		var uuid = $("#taskModen input[name='uuid']").val();
		Otod.ajax({
			url: ctx + '/sys/file/findByBusiId.do',
			data : {"busiId":uuid},
			type : "GET",
			successFn : function(data) {
				if(!data){
					return;
				}
				var sb = new StringBuffer();
				$.each(data,function(i,n){
					sb.append("<li>");
					sb.append("<a href='#' class='fileItem' key='").append(n.uuid).append("'>");
					sb.append(n.fileName);
					sb.append("</a>");
					sb.append("<div class='pull-right'>");
					sb.append("<span class='text-muted'>").append(n.dateFormat).append("</span>");
					if(n.createEmpNo==GOBAL_CUST_NO && ($("#status").val() == "30050002"||$("#status").val == "30050005")){
						
						sb.append("<a href='#' class='fileItemDel' title='删除' key='").append(n.uuid).append("'><i class='icon-trash red bigger-130'></i></a>");
					}
					sb.append("</div>");
					sb.append("</li>");
				});
				$("#taskFileList").html(sb.toString());
			}
		});
	}
	
	
	//显示成果评论意见文档
	function showFruitAttach(){
		var uuid = $("#file-uuid").val();
		Otod.ajax({
			url: ctx + '/sys/file/findByBusiId.do',
			data : {"busiId":uuid},
			type : "GET",
			successFn : function(data) {
				if(!data){
					return;
				}
				var sb = new StringBuffer();
				$.each(data,function(i,n){
					sb.append("<li>");
					sb.append("<a href='#' class='fileItem' key='").append(n.uuid).append("'>");
					sb.append(n.fileName);
					sb.append("</a>");
					sb.append("<div class='pull-right'>");
					sb.append("<span class='text-muted'>").append(n.dateFormat).append("</span>");
					if(n.createEmpNo==GOBAL_CUST_NO){
						
						sb.append("<a href='#' class='fileItemDel' title='删除' key='").append(n.uuid).append("'><i class='icon-trash red bigger-130'></i></a>");
					}
					sb.append("</div>");
					sb.append("</li>");
				});
				$("#comments-doc-list").html(sb.toString());
			}
		});
	}
	//显示任务评论意见文档
	function showTaskCommentAttach(){
		var uuid = $("#taskModen input[name='uuid']").val();
		Otod.ajax({
			url: ctx + '/sys/file/findByBusiId.do',
			data : {"busiId":uuid},
			type : "GET",
			successFn : function(data) {
				if(!data){
					return;
				}
				var sb = new StringBuffer();
				$.each(data,function(i,n){
					sb.append("<li>");
					sb.append("<a href='#' class='fileItem' key='").append(n.uuid).append("'>");
					sb.append(n.fileName);
					sb.append("</a>");
					sb.append("<div class='pull-right'>");
					sb.append("<span class='text-muted'>").append(n.dateFormat).append("</span>");
					if(n.createEmpNo==GOBAL_CUST_NO){
						
						sb.append("<a href='#' class='fileItemDel' title='删除' key='").append(n.uuid).append("'><i class='icon-trash red bigger-130'></i></a>");
					}
					sb.append("</div>");
					sb.append("</li>");
				});
				$("#comments-doc-list-task").html(sb.toString());
			}
		});
	}
	
	
	
	/**
	 * 查询日志记录
	 * @param uuid
	 */
	function showUpdateRecord(uuid){
		Otod.ajax({
			url: ctx + '/sys/log/queryRecord.do',
			data : {"busiId":uuid},
			type : "GET",
			successFn : function(data) {
				
				 readerRecord(data);
			}
		});
	}
		
		
	/**
	 * 载入历史信息
	 * @param data
	 */
	function readerRecord(data){
		
		var showListRecord = $("#showListRecordModel").html();
		
		Mustache.parse(showListRecord);
		var readered = Mustache.render(showListRecord,{
			data:data,
			formatType:function(){
				return function(text,render){
					return changeType(render(text));
				};
			},
			formatDt:function(){
				return function(text,render){
					return DateUtil.dateFormat(+render(text));//+render(text)  加号是把string转换为number类型
				};
			}
		});
		$("#record").html(readered);
	}
	
	/**
	 * 根据项目类型查询模板
	 */
	/*function selectTpPrjType(){
		
		Otod.ajax({
			url: ctx + '/pm/doc/item/getDocTpByPrjType.do',
			data : {"prjType":$("#prjType").val()},
			contentType : "application/x-www-form-urlencoded",
			type : "post",
			successFn : function(data) {
				
				 readerTpPrjType(data);
			}
		});
	}*/
	
	/**
	 * 显示文档模板下拉
	 */
/*	function readerTpPrjType(data){
	var showSelectModel = $("#showSelectModel").html();
		
		Mustache.parse(showSelectModel);
		var readered = Mustache.render(showSelectModel,{
			data:data,			
		});
		$("#selectTpModel").html(readered);
	}*/
	
	/**
	 * 日志类型
	 */
	function changeType(type){
		if(type=="50010001"){
			return "新增";
		}else if(type=="50020001"){
			return "修改";
		}else if(type=="50030001"){
			return "删除";
		}else if(type=="50040001"){
			return "文件下载";
		}else if(type=="50050001"){
			return "文件查看";
		}
	}
	
	
		
